﻿
#include "WebSocketsServer.h"
#include "WebSocketClient.h"
#include <iostream>

class WebSocketsServerEvent:public UWebSocketsServerEvent
{
public:
	WebSocketsServerEvent()
	{

	}
	~WebSocketsServerEvent()
	{

	}
	 void onConnection() {
		std::cout << "onConnection"<<std::endl;
	
	};

	 void onDisconnection(int code) {	
		std::cout << "onDisconnection" << std::endl;	
	};

	 void OnMessage(std::string message, CWebSocketsServer* client) {
	

		std::cout << "OnMessage" << std::endl;
		std::cout << message << std::endl;

	};

	 void onTimeEvent() {
	
	
	
	};

	 void onError(int port) {
	
	};

	 void onError(void* user) {
	
	
	};

};


//回调类
class WebSocketsClientEvent:public UWebSocketsClientEvent
{
public:
	WebSocketsClientEvent()
	{

	}
	~WebSocketsClientEvent()
	{

	}
	//连接成功回调
	virtual void onConnection(bool bSuccess) {
		std::cout << "onConnection" << std::endl;
	
	};

	virtual void onDisconnection() {
		std::cout << "onDisconnection" << std::endl;
	};

	virtual void OnMessage(std::string message, CWebSocketClient* client) {
	
		std::cout << "OnMessage" << std::endl;
		std::cout << message << std::endl;
		//收到啥  发回去啥
		
	
	};

	virtual void onTimeEvent() {
	
	};

	virtual void onError(int port) {
	
	};

	virtual void onError(void* user) {
	
	};
};
#include <windows.h>

int main()
{
	
	//WebSocketsServerEvent callback;
	//CWebSocketsServer test;
	//test.SetEvent(&callback);
	//test.Start(1054);

	//while (true)
	//{
	//	Sleep(1000);
	//	//收到啥  发回去啥
	////	test.sendTextMessage("i am server");
	//}
	//return 0;


	//客户端的测试代码

	WebSocketsClientEvent callback;
	CWebSocketClient  *test=new  CWebSocketClient;
	std::map<std::string, std::string> extraHeaders;
	extraHeaders["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36";
	test->SetEvent(&callback);
	test->SetExtraHeaders(extraHeaders);
	test->Start("ws://49.234.18.41:8866");
	while (true)
	{
		getchar();
		test->sendTextMessage("hellp");
		getchar();
	}

	return 0;




}
